#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time:2020/10/9 9:39 下午
# @Author:boyizhang
from typing import List,Optional

from fastapi import APIRouter,Depends
from app.database import engine, Base, Session
from app.schemals import User, UserType, CreateUserType, CreateUser, CreateProject, Project
from app.crud import UserDao,ProjectDao


# from app.models import Base
Base.metadata.create_all(bind=engine)


router = APIRouter()
userDao = UserDao()
projectDao = ProjectDao()

def get_db():
    db = Session()
    try:
        yield db
    finally:
        db.close()

@router.get("/")
def helloBookKeeping():
    return "hello"


@router.post("/create_usertype",response_model=UserType)
def create_usertype(usertype:UserType,db:Session = Depends(get_db)):
    db_usetype = userDao.cu_usertype(db, usertype)
    return db_usetype
@router.post("/updage_usertype",response_model=UserType)
def update_usertype(usertype:UserType,db:Session = Depends(get_db)):
    print(usertype)
    userDao.cu_usertype(db, usertype)
    return usertype.dict()

@router.get("/ger_usertype_by_id",response_model=UserType)
def ger_usertype_by_id(type_id:int,db:Session =Depends(get_db)):
    return userDao.get_usertype_by_id(db=db,type_id=type_id)

# user
@router.post('/create_user',response_model=User)
def create_user(user:CreateUser,db:Session = Depends(get_db)):
    return userDao.create_user(db,user)

@router.get('/get_user_by_id',response_model=User)
def get_user_by_id(user_id:int,db:Session = Depends(get_db)):
    return userDao.get_user_by_id(db=db,user_id=user_id)

@router.get('/get_all_users',response_model=List[User])
def get_all_users(db:Session = Depends(get_db),pageNum:int=0,pageSize:int =10):
    return userDao.get_all_users(db=db,pageNum=pageNum,pageSize=pageSize)

@router.post('/update_user')
def update_user(user:User,db:Session = Depends(get_db)):
    return userDao.update_user(user=user,db=db)

# project

@router.post('/create_project',response_model=Project,tags=['project'])
def create_project(project:CreateProject,db:Session =Depends(get_db)):
    return projectDao.create_project(db=db,project=project)

@router.get('/get_all_projects',response_model=List[Project],tags=['project'])
def get_all_projects( pageNum: int = 0, pageSize: int = 10,db:Session =Depends(get_db)):
    return projectDao.get_all_project(pageNum=pageNum,pageSize=pageSize,db=db)
@router.post('/update_post',response_model=Project,tags=['project'])
def update_project(project:Project,db:Session =Depends(get_db)):
    return projectDao.update_project(db=db,project=project)
